package com.fishingwithme.android.ui.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fishingwithme.android.data.model.AuditCircle
import com.fishingwithme.android.data.model.CandidateUser
import com.fishingwithme.android.data.repository.FishingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AddCircleAuditViewModel @Inject constructor(
    private val repository: FishingRepository
) : ViewModel() {
    
    // User search states
    private val _userSearchResults = MutableStateFlow<List<CandidateUser>>(emptyList())
    val userSearchResults: StateFlow<List<CandidateUser>> = _userSearchResults.asStateFlow()
    
    private val _isUserSearching = MutableStateFlow(false)
    val isUserSearching: StateFlow<Boolean> = _isUserSearching.asStateFlow()
    
    // Circle search states
    private val _circleSearchResults = MutableStateFlow<List<AuditCircle>>(emptyList())
    val circleSearchResults: StateFlow<List<AuditCircle>> = _circleSearchResults.asStateFlow()
    
    private val _isCircleSearching = MutableStateFlow(false)
    val isCircleSearching: StateFlow<Boolean> = _isCircleSearching.asStateFlow()
    
    // Submission states
    private val _isSubmitting = MutableStateFlow(false)
    val isSubmitting: StateFlow<Boolean> = _isSubmitting.asStateFlow()
    
    private val _submitResult = MutableStateFlow<SubmitResult?>(null)
    val submitResult: StateFlow<SubmitResult?> = _submitResult.asStateFlow()
    
    sealed class SubmitResult {
        data class Success(val message: String) : SubmitResult()
        data class Error(val message: String) : SubmitResult()
    }
    
    fun searchUsers(nickname: String) {
        viewModelScope.launch {
            _isUserSearching.value = true
            try {
                repository.searchAuditCandidates(nickname).collect { result ->
                    if (result.isSuccess) {
                        _userSearchResults.value = result.getOrNull() ?: emptyList()
                    } else {
                        _userSearchResults.value = emptyList()
                    }
                    _isUserSearching.value = false
                }
            } catch (e: Exception) {
                _userSearchResults.value = emptyList()
                _isUserSearching.value = false
            }
        }
    }
    
    fun searchCircles(title: String) {
        viewModelScope.launch {
            _isCircleSearching.value = true
            try {
                repository.searchCircleCandidates(title).collect { result ->
                    if (result.isSuccess) {
                        _circleSearchResults.value = result.getOrNull() ?: emptyList()
                    } else {
                        _circleSearchResults.value = emptyList()
                    }
                    _isCircleSearching.value = false
                }
            } catch (e: Exception) {
                _circleSearchResults.value = emptyList()
                _isCircleSearching.value = false
            }
        }
    }
    
    fun submitAudit(
        nickname: String,
        userId: String,
        circleName: String,
        circleId: Long
    ) {
        viewModelScope.launch {
            _isSubmitting.value = true
            try {
                repository.addCircleAudit(nickname, userId, circleName, circleId, false).collect { result ->
                    if (result.isSuccess) {
                        _submitResult.value = SubmitResult.Success("Audit added successfully")
                    } else {
                        _submitResult.value = SubmitResult.Error(result.exceptionOrNull()?.message ?: "Failed to add audit")
                    }
                    _isSubmitting.value = false
                }
            } catch (e: Exception) {
                _submitResult.value = SubmitResult.Error(e.message ?: "Failed to add audit")
                _isSubmitting.value = false
            }
        }
    }
    
    fun clearSubmitResult() {
        _submitResult.value = null
    }
}